package com.cpi.cornpp.period.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.period.dto.PeriodDTO;
import com.cpi.cornpp.period.view.PeriodView;
import com.cs.common.utils.StringUtils;

public class PeriodConverter {
	
	public final PeriodView convert(PeriodDTO periodDTO){
		PeriodView periodView = null;
		
		if (periodDTO != null){
			periodView = new PeriodView();
			
			periodView.setPeriodId(periodDTO.getPeriodId());
			periodView.setPeriodName(new StringBuffer(periodDTO.getShortName()).append(StringUtils.SPACE_CHAR).append(periodDTO.getYear()).toString());		
			periodView.setPeriodDescription(periodDTO.getDescription());
			periodView.setPeriodStartMonth(periodDTO.getStartMonth());
			periodView.setPeriodEndMonth(periodDTO.getEndMonth());
			periodView.setPeriodActive((periodDTO.getActive() == 1));
			periodView.setPeriodYear(periodDTO.getYear());
		}
		
		return periodView;
	}
	
	public final PeriodDTO convert(PeriodView periodView){
		PeriodDTO periodDTO = null;
		
		if (periodView != null){
			periodDTO = new PeriodDTO();
			
			periodDTO.setPeriodId(periodView.getPeriodId());
			periodDTO.setShortName(periodView.getPeriodName());
			periodDTO.setDescription(periodView.getPeriodDescription());
			periodDTO.setStartMonth(periodView.getPeriodStartMonth());
			periodDTO.setEndMonth(periodView.getPeriodEndMonth());
			periodDTO.setActive((periodView.isPeriodActive()) ? 1 : 0);
			periodDTO.setYear(periodView.getPeriodYear());
		}
		
		return periodDTO;
	}
	
	public final List convertViews(List periodViews){
		List periods = null;
		
		periods = new ArrayList();
		
		if (!CollectionUtils.isEmpty(periodViews)){
			Iterator iterator = periodViews.iterator();
			PeriodView view = null;
			while(iterator.hasNext()){
				view = (PeriodView)iterator.next();
				periods.add(convert(view));
			}
		}
		
		return periods;
	}
	
	public final List convertDTO(List periodDTO){
		List periods = null;
		
		periods = new ArrayList(0);
		
		if (!CollectionUtils.isEmpty(periodDTO)){
			Iterator iterator = periodDTO.iterator();
			PeriodDTO dto = null;
			while(iterator.hasNext()){
				dto = (PeriodDTO)iterator.next();
				periods.add(convert(dto));
			}
		}
		
		return periods;
	}
}